package leetcode.题库.二叉树;

import leetcode.题库.pubclass.ListNode;
import leetcode.题库.pubclass.TreeNode;

import java.util.*;

/**
 * 二叉树的层序遍历
 *
 * @author yanzhenming
 * @date 2023/4/16  2:52
 */
public class Solution102 {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            List<Integer> levList = new ArrayList<>();
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = q.poll();
                levList.add(node.val);
                if(node.left != null) {
                    q.offer(node.left);
                }
                if(node.right != null) {
                    q.offer(node.right);
                }
            }
            res.add(levList);
        }
        return res;
    }

}
